package com.share.homestay.utils;

import com.share.homestay.pojo.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.csrf.CsrfException;

public class AuthExceptionUtil {
    public static Result  getErrMsgByExceptionType(AuthenticationException e) {
        if (e instanceof LockedException) {
            return Result.failure(1100, "账户被锁定，请联系管理员!");
        } else if (e instanceof CredentialsExpiredException) {
            return Result.failure(1105,"用户名或者密码输入错误!");
        }else if (e instanceof InsufficientAuthenticationException) {
            return Result.failure(403,"权限不足请登录!");
        } else if (e instanceof AccountExpiredException) {
            return Result.failure(1101, "账户过期，请联系管理员!");
        } else if (e instanceof DisabledException) {
            return Result.failure(1102, ("账户被禁用，请联系管理员!"));
        } else if (e instanceof BadCredentialsException) {
            return Result.failure(1105, "用户名或者密码输入错误!");
        }else if (e instanceof AuthenticationServiceException) {
            return Result.failure(1106, "认证失败，请重试!");
        }

        return Result.failure(1200, e.getMessage());
    }
    public static Result getErrMsgByExceptionType(AccessDeniedException e) {
        if (e instanceof CsrfException) {
            return Result.failure(-1001, "非法访问跨域请求异常!");
        } else if (e instanceof CsrfException) {
            return Result.failure(-1002,"非法访问跨域请求异常!");
        } else if (e instanceof AuthorizationServiceException) {
            return Result.failure(1101, "认证服务异常请重试!");
        }else if (e instanceof AccessDeniedException) {
            return Result.failure(4003, "权限不足不允许访问!");
        }

        return Result.failure(1200, e.getMessage());
    }
}
